<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/" style="margin-bottom: 15px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- 头部按钮 -->
      <div class="clearfix">
        <el-button
          @click="dialogTableVisible = true"
          type="primary"
          style="float: left; margin-bottom: 18px"
        >
          添加角色
        </el-button>
      </div>
      <!-- 内容 -->
      <div class="text item">
        <el-table stripe :data="form" style="width: 100%" border>
          <el-table-column width="50">
            <template>
              <i style="margin-left: 8px" class="el-icon-arrow-right"></i>
            </template>
          </el-table-column>
          <el-table-column type="index" width="50"></el-table-column>
          <el-table-column prop="roleName" label="角色名称" width="595">
          </el-table-column>
          <el-table-column prop="roleDesc" label="角色描述" width="595">
          </el-table-column>
          <el-table-column label="操作" width="347">
            <template #default="{ row }">
              <el-button
                @click="echo(row)"
                style="width: 90px"
                type="primary"
                icon="el-icon-edit"
                >编辑</el-button
              >
              <el-button
                @click="del(row)"
                style="width: 90px"
                type="danger"
                icon="el-icon-delete"
                >删除</el-button
              >
              <el-button style="width: 110px" type="warning" icon="el-icon-menu"
                >分配权限</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加用户弹出框 -->
      <el-dialog title="添加用户" :visible.sync="dialogTableVisible">
        <el-form :model="formIs" ref="formIs" :rules="rules">
          <el-form-item
            prop="roleName"
            label="角色名称"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model.trim="formIs.roleName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item
            prop="roleDesc"
            label="角色描述"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model.trim="formIs.roleDesc"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addUp('remove')">取 消</el-button>
          <el-button type="primary" @click="addUp('add')">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 编辑弹框 -->
      <el-dialog title="修改用户" :visible.sync="editEcho">
        <el-form :model="formIs" ref="formIs" :rules="rules">
          <el-form-item
            prop="roleName"
            label="角色名称"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model.trim="formIs.roleName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item
            prop="roleDesc"
            label="角色描述"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model.trim="formIs.roleDesc"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="btnEdit('two')">取 消</el-button>
          <el-button type="primary" @click="btnEdit('one')">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { roleList, addRoleList, editList, deleList } from '@/api/jurisdiction'
import { Message } from 'element-ui'
export default {
  name: 'my-roles',
  data () {
    return {
      form: [], // 角色列表
      // 校验
      formIs: {
        roleName: '',
        roleDesc: ''
      },
      dialogTableVisible: false, // 添加弹框显示隐藏
      editEcho: false,
      formLabelWidth: '120px', // 弹出卡片尺寸
      rules: {
        roleName: [
          {
            required: true,
            message: '请输入角色名称'
          }
        ],
        roleDesc: [
          {
            required: true,
            message: '请输入角色描述'
          }
        ]
      }
    }
  },

  created () {
    this.roleList()
  },

  methods: {
    // 角色列表
    async roleList () {
      const res = await roleList()
      // console.log(res.data)
      this.form = res.data
    },
    // 添加用户
    addUp (value) {
      if (value === 'add') {
        this.$refs.formIs.validate(async (blo) => {
          console.log(blo)
          if (blo) {
            const res = await addRoleList({
              id: this.formIs.id,
              roleName: this.formIs.roleName,
              roleDesc: this.formIs.roleDesc
            })
            // console.log(res)
            if (res.meta.status === 201) {
              this.roleList()
              Message.success(res.meta.msg)
              this.dialogTableVisible = false
              this.$refs.formIs.resetFields()
            }
          } else {
            return false
          }
        })
      } else if (value === 'remove') {
        this.dialogTableVisible = false
        this.$refs.formIs.resetFields()
        Message('取消添加')
      }
    },
    // 回显
    echo (row) {
      this.editEcho = true
      this.formIs = row
    },
    // 修改
    async btnEdit (val) {
      if (val === 'one') {
        const res = await editList(this.formIs.id, this.formIs)
        console.log(res)
        if (res.meta.status === 200) {
          this.roleList()
          this.editEcho = false
          Message.success('修改成功')
        } else {
          Message.error(res.meta.msg)
          this.editEcho = false
        }
      } else if (val === 'two') {
        Message('取消修改')
        this.editEcho = false
      }
    },
    // 删除确认框
    del (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await deleList(row.id)
          this.roleList()
          Message.success(res.meta.msg)
        })
        .catch(() => {
          Message('取消删除')
        })
    }
  }
}
</script>

<style scoped lang='scss'>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
</style>
